package com.hongxun.modules.bss.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.config.PriceConfig;
import com.hongxun.modules.bss.entity.*;
import com.hongxun.modules.bss.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 项目估算表
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-08-03 13:46:04
 */
@RestController
@RequestMapping("/bss/apestimate")
public class APEstimateController {
	@Autowired
	private APEstimateService aPEstimateService;
	@Autowired
	private APBaseService aPBaseService;
	@Autowired
	private APCostService aPCostService;
	@Autowired
	private DCrossCostService dCrossCostService;
	@Autowired
	private DMaterialBuyService dMaterialBuyService;
	@Autowired
	private DDeviceBuyService dDeviceBuyService;
	@Autowired
	private DMaterialCostService dMaterialCostService;
	@Autowired
	private DOtherCostService dOtherCostService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("state")!=null&&query.get("state").toString().equals("isdelno")) {
			wheres += " and  p_e_datastate != '删除' ";
		}
        query.put("filterSql",wheres+" ");
		List<APEstimateEntity> aPEstimateList = aPEstimateService.queryList(query);
		int total = aPEstimateService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(aPEstimateList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		APEstimateEntity aPEstimate = aPEstimateService.queryObject(guid);
		
		return R.ok().put("aPEstimate", aPEstimate);
	}
	//修改计算但不保存
	@RequestMapping("/calculation")
	public R calculation(@RequestBody APEstimateEntity aPEstimate){
		countPrice(aPEstimate);
      	return R.ok().put("aPEstimate", aPEstimate);
	}

	private void countPrice(@RequestBody APEstimateEntity aPEstimate) {
		BigDecimal tiole =BigDecimal.ZERO;

		//计算其他费用
		if(PriceConfig.otherPricetole.equals(aPEstimate.getPEItem()))
		{
			tiole = getqtfy(aPEstimate, tiole);
		}
		//计算交叉跨越费
		if(PriceConfig.jckyPricetole.equals(aPEstimate.getPEItem()))
		{
			tiole = getjcky(aPEstimate, tiole);
			tiole = tiole.divide(new BigDecimal("10000"));

		}
		//计算材料购置费
		if(PriceConfig.clgzPricetole.equals(aPEstimate.getPEItem()))
		{
			tiole = getclgz(aPEstimate, tiole);
			tiole = tiole.divide(new BigDecimal("10000"));
		}
		//计算设备购置费
		if(PriceConfig.scgzPricetole.equals(aPEstimate.getPEItem()))
		{
			tiole = getsbgz(aPEstimate, tiole);
			tiole = tiole.divide(new BigDecimal("10000"));
		}
		//计算安装费
		if(PriceConfig.azPricetole.equals(aPEstimate.getPEItem()))
		{
			tiole = getgcf(aPEstimate, tiole,"1");
			//其他工程量
			tiole = getqtgcf(aPEstimate, tiole,"1");
			tiole = tiole.divide(new BigDecimal("10000"));
		}
		//计算拆除费
		if(PriceConfig.cjgcPricetole.equals(aPEstimate.getPEItem()))
		{
			tiole = getgcf(aPEstimate, tiole,"2");
			//其他工程量
			tiole = getqtgcf(aPEstimate, tiole,"2");
			tiole = tiole.divide(new BigDecimal("10000"));
		}
		//计算大修费
		if(PriceConfig.dxPricetole.equals(aPEstimate.getPEItem()))
		{
			//工程量
			tiole = getgcf(aPEstimate, tiole,"3");
			//其他工程量
			tiole = getqtgcf(aPEstimate, tiole,"3");

			tiole = tiole.divide(new BigDecimal("10000"));

		}

		aPEstimate.setPEInitial(tiole.setScale(4, BigDecimal.ROUND_HALF_UP));
		BigDecimal	alll =aPEstimate.getPEInitial().multiply(aPEstimate.getPEOwnRatio()).multiply(aPEstimate.getPESysRatio());
		aPEstimate.setPEFinal(alll.setScale(4, BigDecimal.ROUND_HALF_UP) );
	}

	//计算全部计算
	@RequestMapping("/updateall")
	public R updateall(@RequestBody String   guid) {
		Map<String, Object> mapaPOther = new HashMap<>();
		String wheres = " 1=1 ";
		wheres += " and  p_guid = '" + guid + "' ";
		mapaPOther.put("filterSql", wheres + " ");
		List<APEstimateEntity> aPEstimateList = aPEstimateService.queryList(mapaPOther);
		for (APEstimateEntity aPEstimate : aPEstimateList) {
			countPrice(aPEstimate);

			aPEstimate.setPEDatastate("修改");
			aPEstimate.setPEChangetime(new Date());
			aPEstimateService.update(aPEstimate);
        }
		APBaseEntity aPBaseEntity = aPBaseService.queryObject(guid);
		aPBaseEntity.setPBDatastate("修改");
		aPBaseEntity.setPBState("20");
		aPBaseEntity.setPBChangetime(new Date());
		aPBaseService.update(aPBaseEntity);
		return R.ok();
	}
	 //单个直接计算
	@RequestMapping("/calculationone")
	public R calculationone( @RequestBody String   guid){
		//String guid = request.getParameter("guid");
		APEstimateEntity	aPEstimate =	aPEstimateService.queryObject(guid);
		countPrice(aPEstimate);
		aPEstimate.setPEDatastate("修改");
		aPEstimate.setPEChangetime(new Date());
		aPEstimateService.update(aPEstimate);
		APBaseEntity aPBaseEntity = aPBaseService.queryObject(aPEstimate.getPGuid());
		aPBaseEntity.setPBDatastate("修改");
		aPBaseEntity.setPBState("20");
		aPBaseEntity.setPBChangetime(aPEstimate.getPEChangetime());
		aPBaseService.update(aPBaseEntity);
		return R.ok();
	}


	private BigDecimal getgcf(@RequestBody APEstimateEntity aPEstimate, BigDecimal tiole,String types) {
		Map<String ,Object> mapaPOther = new HashMap<>();

		String wheres =" 1=1 ";

		wheres+=" and  p_guid = '" + aPEstimate.getPGuid()+"' ";
		wheres+=" and  m_c_Work = '" + types+"' ";
		wheres+=" and  m_c_datastate != '删除' ";
		mapaPOther.put("filterSql",wheres+" ");
		List<DMaterialCostEntity> dMaterialCostList = dMaterialCostService.queryList(mapaPOther);
		for(DMaterialCostEntity dMaterialCostEntity :dMaterialCostList)
		{
			tiole=tiole.add(dMaterialCostEntity.getMCPriceall()==null?BigDecimal.ZERO:dMaterialCostEntity.getMCPriceall());
		}
		return tiole;
	}

	private BigDecimal getqtgcf(@RequestBody APEstimateEntity aPEstimate, BigDecimal tiole,String types) {
		Map<String ,Object> mapaPOther = new HashMap<>();

		String wheres =" 1=1 ";

		wheres+=" and  p_guid = '" + aPEstimate.getPGuid()+"' ";
		wheres+=" and  o_c_Work = '" + types+"' ";
		wheres+=" and  c_c_datastate != '删除' ";
		mapaPOther.put("filterSql",wheres+" ");
		List<DOtherCostEntity> dOtherCostList = dOtherCostService.queryList(mapaPOther);
		for(DOtherCostEntity dOtherCostEntity :dOtherCostList)
		{
			tiole=tiole.add(dOtherCostEntity.getOCPriceall()==null?BigDecimal.ZERO:dOtherCostEntity.getOCPriceall());
		}
		return tiole;
	}

	private BigDecimal getsbgz(@RequestBody APEstimateEntity aPEstimate, BigDecimal tiole) {
		Map<String ,Object> mapaPOther = new HashMap<>();

		String wheres =" 1=1 ";

		wheres+=" and  p_guid = '" + aPEstimate.getPGuid()+"' ";

		wheres+=" and  d_b_datastate != '删除' ";

		mapaPOther.put("filterSql",wheres+" ");
		List<DDeviceBuyEntity> dDeviceBuyList = dDeviceBuyService.queryList(mapaPOther);
		for(DDeviceBuyEntity dDeviceBuyEntity :dDeviceBuyList)
		{
			tiole=tiole.add(dDeviceBuyEntity.getDBPriceall()==null?BigDecimal.ZERO:dDeviceBuyEntity.getDBPriceall());
		}
		return tiole;
	}
	private BigDecimal getclgz(@RequestBody APEstimateEntity aPEstimate, BigDecimal tiole) {
		Map<String ,Object> mapaPOther = new HashMap<>();

		String wheres =" 1=1 ";
        wheres+=" and  p_guid = '" + aPEstimate.getPGuid()+"' ";
        wheres+=" and  m_b_datastate != '删除' ";
		mapaPOther.put("filterSql",wheres+" ");

		List<DMaterialBuyEntity> dMaterialBuyList = dMaterialBuyService.queryList(mapaPOther);

		for(DMaterialBuyEntity dMaterialBuyEntity :dMaterialBuyList)
		{
             tiole=tiole.add(dMaterialBuyEntity.getMBPriceall()==null?BigDecimal.ZERO:dMaterialBuyEntity.getMBPriceall());
		}
		return tiole;
	}

	private BigDecimal getjcky (@RequestBody APEstimateEntity aPEstimate, BigDecimal tiole) {
		Map<String ,Object> mapaPOther = new HashMap<>();

		String wheres =" 1=1 ";
		wheres+=" and  p_guid = '" + aPEstimate.getPGuid()+"' ";
		wheres+=" and  c_c_datastate != '删除' ";
		mapaPOther.put("filterSql",wheres+" ");
		List<DCrossCostEntity> dCrossCostList = dCrossCostService.queryList(mapaPOther);

		for(DCrossCostEntity dCrossCostEntity :dCrossCostList)
        {

            tiole=tiole.add(dCrossCostEntity.getCCPrice()==null?BigDecimal.ZERO:dCrossCostEntity.getCCPrice());

        }
		return tiole;
	}

	private BigDecimal getqtfy(@RequestBody APEstimateEntity aPEstimate, BigDecimal tiole) {
		Map<String ,Object> mapaPOther = new HashMap<>();
		String aPCostwheres =" 1=1 ";
		aPCostwheres+=" and  p_guid = '" + aPEstimate.getPGuid()+"' ";
		aPCostwheres += " and  p_c_datastate != '删除' ";
		mapaPOther.put("filterSql",aPCostwheres+" ");
		List<APCostEntity> aPCostList = aPCostService.queryList(mapaPOther);
		for(APCostEntity aPCostEntity :aPCostList)
        {
            if(aPCostEntity.getPCToll().equals("是"))
            {
                tiole=tiole.add(aPCostEntity.getPCPrice()==null?BigDecimal.ZERO:aPCostEntity.getPCPrice());
            }
        }
		return tiole;
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody APEstimateEntity aPEstimate){
		aPEstimateService.save(aPEstimate);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody APEstimateEntity aPEstimate){
		aPEstimate.setPEDatastate("修改");
		aPEstimate.setPEChangetime(new Date());
		aPEstimateService.update(aPEstimate);
		APBaseEntity aPBaseEntity = aPBaseService.queryObject(aPEstimate.getPGuid());
		aPBaseEntity.setPBDatastate("修改");
		aPBaseEntity.setPBState("20");
		aPBaseEntity.setPBChangetime(aPEstimate.getPEChangetime());
		aPBaseService.update(aPBaseEntity);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		aPEstimateService.deleteBatch(guids);
		
		return R.ok();
	}

}
